package nz.ac.massey.cs.webtech.s_15273550.server;

import java.util.List;

public class GamingRoom {

    public Player getPlayer() {
        return player;
    }

    public Dealer getDealer() {
        return dealer;
    }

    public Deck getDeck() {
        return deck;
    }

    private Player player;
    private Dealer dealer;
    private Deck deck;
    private String turn;

    public String getTurn() {
        return turn;
    }

    public void setTurn(String turn) {
        this.turn = turn;
    }

    public GamingRoom() {
        player = new Player();
        player.setMoney(1000);
        dealer = new Dealer();
        deck = new Deck();
        turn = "user";
    }

    public void startGame() {
        play();
    }

    //start
    private void play() {
        //shuffle
        deck.shuffle();
        //clear
        player.clear();
        dealer.clear();

        //dealer's turn
        dealer.getCardsInHand().add(deck.getOne());
        dealer.getCardsInHand().add(deck.getOne());
        dealer.showCardsWithHidden();
        //player's turn
        player.getCardsInHand().add(deck.getOne());
        player.getCardsInHand().add(deck.getOne());
        player.showCards();
    }

    public void dealerStep() {
        if (dealer.getTotalPoints() <= 17 && !BlackJackRule.isPlayerOver(player)) {
            System.out.println("dealer hit");
            dealer.getCardsInHand().add(deck.getOne());
            dealer.showCards();
        }
        System.out.println("dealer stand");
        setTurn("user");
    }

    public String decideWinner(Player player, Dealer dealer) {
        //
        List<Card> playerCards = player.getCardsInHand();
        int playerSum = player.getTotalPoints();

        if (playerSum > 21) {
            return "computer";
        }
        boolean isPlayerBlackJack = false;
        if (playerSum == 21 && playerCards.size() == 2) {
            isPlayerBlackJack = true;
        }
        //
        List<Card> dealerCards = dealer.getCardsInHand();
        int dealerSum = dealer.getTotalPoints();

        if (dealerSum > 21) {
            return "user";
        }
        boolean isDealerBlackJack = false;
        if (dealerCards.size() == 2 && dealerSum == 21) {
            isDealerBlackJack = true;
        }
        //
        if (isPlayerBlackJack) {
            if (isDealerBlackJack) {
                return "none";
            } else {
                return "user";
            }
        } else {
            if (playerSum > dealerSum) {
                return "user";
            } else if (playerSum == dealerSum) {
                return "none";
            } else {
                return "computer";
            }
        }
    }
}


